package es.project.sien7e.main.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import es.project.sien7e.main.model.Agenda;
import es.project.sien7e.main.model.Directorio;
import es.project.sien7e.main.model.Foto;
import es.project.sien7e.main.model.Menu;
import es.project.sien7e.main.model.Municipio;
import es.project.sien7e.main.model.Provincia;
import es.project.sien7e.main.model.Rol;
import es.project.sien7e.main.model.Rolmenu;
import es.project.sien7e.main.model.Tipodirectorio;
import es.project.sien7e.main.model.Usuario;
import es.project.sien7e.main.model.Usuariorol;
import es.project.sien7e.main.services.DAOService;

@Controller
// @SessionAttributes("usuario")
@RequestMapping("administracion")
public class AdministracionController {
	private DAOService daoService;

	@Autowired
	public void setDaoService(DAOService daoService) {
		this.daoService = daoService;
	}

	@RequestMapping("/")
	public String listaRaiz(@Valid Usuario obj, BindingResult result,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Usuario> list = daoService.getUsuarios();
		request.setAttribute("usuarios", list);
		request.setAttribute("idMenu", daoService.getMenu("Usuario"));
		return "usuarios";
	}

	@RequestMapping("/agenda/lista")
	public String lista(@Valid Agenda obj, BindingResult result,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Agenda> list = daoService.getAgendas();
		List<Provincia> list2 = daoService.getListadoProvincias();
		List<Municipio> list3 = daoService.getMunicipios(null);
		request.setAttribute("list", list);
		request.setAttribute("provincias", list2);
		request.setAttribute("municipios", list3);
		request.setAttribute("idMenu", daoService.getMenu("Agenda"));
		return "agenda";
	}

	@RequestMapping("/directorio/lista")
	public String lista(@Valid Directorio obj, BindingResult result,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Directorio> list = daoService.getDirectorios();
		List<Tipodirectorio> list2 = daoService.getListaTiposdirectorios();
		request.setAttribute("list", list);
		request.setAttribute("tiposDirectorios", list2);
		request.setAttribute("idMenu", daoService.getMenu("Directorio"));
		return "directorio";
	}

	@RequestMapping("/tipodirectorio/lista")
	public String lista(@Valid Tipodirectorio obj, BindingResult result,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Tipodirectorio> list = daoService.getTiposdirectorios();
		request.setAttribute("list", list);
		request.setAttribute("idMenu", daoService.getMenu("Tipodirectorio"));
		return "tipodirectorio";
	}

	@RequestMapping("/menu/lista")
	public String lista(@Valid Menu obj, BindingResult result,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Menu> list = daoService.getMenus();
		List<Menu> list2 = daoService.getMenusPadres();
		request.setAttribute("list", list);
		request.setAttribute("padres", list2);
		request.setAttribute("idMenu", daoService.getMenu("Menu"));
		return "menu";
	}

	@RequestMapping("/usuario/lista")
	public String lista(@Valid Usuario obj, BindingResult result,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Usuario> list = daoService.getUsuarios();
		List<Rol> list2 = daoService.getListaRoles();
		request.setAttribute("usuarios", list);
		request.setAttribute("roles", list2);
		request.setAttribute("idMenu", daoService.getMenu("Usuario"));
		return "usuarios";
	}

	@RequestMapping("/rol/lista")
	public String lista(@Valid Rol obj, BindingResult result,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Rol> list = daoService.getRoles();
		request.setAttribute("list", list);
		request.setAttribute("idMenu", daoService.getMenu("Rol"));
		return "rol";
	}

	@RequestMapping("/foto/lista")
	public String lista(@Valid Foto obj, BindingResult result,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Foto> list = daoService.getFotos();
		List<Directorio> list2 = daoService.getListaDirectorios();
		request.setAttribute("list", list);
		request.setAttribute("directorios", list2);
		request.setAttribute("idMenu", daoService.getMenu("Foto"));
		return "foto";
	}

	@RequestMapping("/usuariorol/lista")
	public String lista(@Valid Usuariorol obj, BindingResult result,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Usuariorol> list = daoService.getUsuariorols();
		List<Rol> list2 = daoService.getListaRoles();
		List<Usuario> list3 = daoService.getListaUsuarios();
		request.setAttribute("list", list);
		request.setAttribute("roles", list2);
		request.setAttribute("usuarios", list3);
		request.setAttribute("idMenu", daoService.getMenu("Permisos Usuarios"));
		return "usuariorol";
	}

	@RequestMapping("/rolmenu/lista")
	public String lista(@Valid Rolmenu obj, BindingResult result,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Rolmenu> list = daoService.getRolmenus();
		List<Rol> list2 = daoService.getListaRoles();
		List<Menu> list3 = daoService.getListaMenus();
		request.setAttribute("list", list);
		request.setAttribute("roles", list2);
		request.setAttribute("menus", list3);
		request.setAttribute("idMenu", daoService.getMenu("Permisos Menu"));
		return "rolmenu";
	}

	@RequestMapping("/provincia/lista")
	public String lista(@Valid Provincia obj, BindingResult result,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Provincia> list = daoService.getProvincias();
		request.setAttribute("list", list);
		request.setAttribute("idMenu", daoService.getMenu("Provincias"));
		return "provincias";
	}

	@RequestMapping("/municipio/lista")
	public String lista(@Valid Municipio obj, BindingResult result,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Municipio> list = daoService.getMunicipios();
		List<Provincia> list2 = daoService.getListadoProvincias();
		request.setAttribute("list", list);
		request.setAttribute("provincias", list2);
		request.setAttribute("idMenu", daoService.getMenu("Municipios"));
		return "municipios";
	}
}
